{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from redbaron import RedBaron\n",
    "from collections import defaultdict\n",
    "from collections.abc import Iterable\n",
    "import abc\n",
    "from typing import Any, Optional \n",
    "from dataclasses import dataclass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dataclass      \n",
    "class BasicVarInfo:\n",
    "    path: list[int|str] = None\n",
    "    type: abc.ABCMeta = None\n",
    "    value: Any = None\n",
    "    nargs: str|int = 1 \n",
    "    help: Optional[str] = None\n",
    "    var: list[str] = None\n",
    "    \n",
    "    def __post_init__(self):\n",
    "        self.value: Optional[self.type]\n",
    "        self.name = self.var[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 -----------------------------------------------------\n",
      "\u001b[38;5;148mStringNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: string, string_, stringnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'\"\"\"Convert Qwen2-hf ckpt to Megatron-LM ckpt.\"\"\"'\u001b[39m\n",
      "1 -----------------------------------------------------\n",
      "\u001b[38;5;148mEndlNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: endl, endl_, endlnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'\\n'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mindent\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m''\u001b[39m\n",
      "2 -----------------------------------------------------\n",
      "\u001b[38;5;148mFromImportNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: from_import, from_import_, fromimport, fromimportnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# helpers: full_path_modules, full_path_names, modules, names\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m NameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'lingo'\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m NameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'utils'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mtargets\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m NameAsNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: name_as_name, name_as_name_, nameasname, nameasnamenode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'ModelType'\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m''\u001b[39m\n",
      "3 -----------------------------------------------------\n",
      "\u001b[38;5;148mEndlNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: endl, endl_, endlnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'\\n'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mindent\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m''\u001b[39m\n",
      "4 -----------------------------------------------------\n",
      "\u001b[38;5;148mAssignmentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: assign, assignment, assignment_, assignmentnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15moperator\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m''\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'misc'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mannotation\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'dict'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mAtomtrailersNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: atomtrailers, atomtrailers_, atomtrailersnode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m NameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m CallNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "5 -----------------------------------------------------\n",
      "\u001b[38;5;148mEndlNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: endl, endl_, endlnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'\\n'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mindent\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m''\u001b[39m\n"
     ]
    }
   ],
   "source": [
    "with open('/mnt/user/linzhixin/github/tools/ez_config/test_config.py', 'r') as source_code:\n",
    "    red = RedBaron(source_code.read())\n",
    "\n",
    "red.help()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 -----------------------------------------------------\n",
      "\u001b[38;5;148mCallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'type'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'BertCheckpointConvertor'\u001b[39m\n",
      "1 -----------------------------------------------------\n",
      "\u001b[38;5;148mCommaNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: comma, comma_, commanode\u001b[39m\n",
      "2 -----------------------------------------------------\n",
      "\u001b[38;5;148mCallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'loader_dict'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mAtomtrailersNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: atomtrailers, atomtrailers_, atomtrailersnode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m NameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m CallNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "3 -----------------------------------------------------\n",
      "\u001b[38;5;148mCommaNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: comma, comma_, commanode\u001b[39m\n",
      "4 -----------------------------------------------------\n",
      "\u001b[38;5;148mCallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'mgt_path'\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m      \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'None'\u001b[39m\n",
      "5 -----------------------------------------------------\n",
      "\u001b[38;5;148mCommaNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: comma, comma_, commanode\u001b[39m\n"
     ]
    }
   ],
   "source": [
    "from redbaron import FromImportNode, AssignmentNode\n",
    "\n",
    "for line in red:\n",
    "    if isinstance(line, AssignmentNode):\n",
    "        line.value.value[1].value.help()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'str' object has no attribute 'value'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 26\u001b[0m\n\u001b[1;32m     23\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m     24\u001b[0m         save[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m-\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(var)] \u001b[38;5;241m=\u001b[39m (path, var, red\u001b[38;5;241m.\u001b[39mvalue)\n\u001b[0;32m---> 26\u001b[0m \u001b[43mdfs\u001b[49m\u001b[43m(\u001b[49m\u001b[43massignment\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     27\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m key, value \u001b[38;5;129;01min\u001b[39;00m save\u001b[38;5;241m.\u001b[39mitems():\n\u001b[1;32m     28\u001b[0m     \u001b[38;5;28mprint\u001b[39m(key, value)\n",
      "Cell \u001b[0;32mIn[5], line 18\u001b[0m, in \u001b[0;36mdfs\u001b[0;34m(red, path, var)\u001b[0m\n\u001b[1;32m     16\u001b[0m     path\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m     17\u001b[0m     \u001b[38;5;66;03m# print('value深入')\u001b[39;00m\n\u001b[0;32m---> 18\u001b[0m     \u001b[43mdfs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mred\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvar\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     19\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(red, Iterable): \n\u001b[1;32m     20\u001b[0m     \u001b[38;5;66;03m# print('可迭代')\u001b[39;00m\n\u001b[1;32m     21\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m index, i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(red):\n",
      "Cell \u001b[0;32mIn[5], line 22\u001b[0m, in \u001b[0;36mdfs\u001b[0;34m(red, path, var)\u001b[0m\n\u001b[1;32m     19\u001b[0m     \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(red, Iterable): \n\u001b[1;32m     20\u001b[0m         \u001b[38;5;66;03m# print('可迭代')\u001b[39;00m\n\u001b[1;32m     21\u001b[0m         \u001b[38;5;28;01mfor\u001b[39;00m index, i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(red):\n\u001b[0;32m---> 22\u001b[0m             \u001b[43mdfs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mindex\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvar\u001b[49m\u001b[43m[\u001b[49m\u001b[43m:\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     23\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m     24\u001b[0m     save[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m-\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(var)] \u001b[38;5;241m=\u001b[39m (path, var, red\u001b[38;5;241m.\u001b[39mvalue)\n",
      "Cell \u001b[0;32mIn[5], line 18\u001b[0m, in \u001b[0;36mdfs\u001b[0;34m(red, path, var)\u001b[0m\n\u001b[1;32m     16\u001b[0m     path\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m     17\u001b[0m     \u001b[38;5;66;03m# print('value深入')\u001b[39;00m\n\u001b[0;32m---> 18\u001b[0m     \u001b[43mdfs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mred\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvar\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     19\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(red, Iterable): \n\u001b[1;32m     20\u001b[0m     \u001b[38;5;66;03m# print('可迭代')\u001b[39;00m\n\u001b[1;32m     21\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m index, i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(red):\n",
      "Cell \u001b[0;32mIn[5], line 24\u001b[0m, in \u001b[0;36mdfs\u001b[0;34m(red, path, var)\u001b[0m\n\u001b[1;32m     22\u001b[0m             dfs(i, path\u001b[38;5;241m=\u001b[39mpath\u001b[38;5;241m+\u001b[39m[index], var\u001b[38;5;241m=\u001b[39mvar[:])\n\u001b[1;32m     23\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m---> 24\u001b[0m     save[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m-\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(var)] \u001b[38;5;241m=\u001b[39m (path, var, \u001b[43mred\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m)\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'value'"
     ]
    }
   ],
   "source": [
    "def redbaron_config_parser(red: RedBaron) -> dict[str, BasicVarInfo]:\n",
    "    ret = {}\n",
    "    def dfs(red, path=[], var=[]):\n",
    "        if hasattr(red, 'target'):\n",
    "            # print('有target')\n",
    "            # print('路径:', red.target.value)\n",
    "            path.append('value')\n",
    "            var.append(red.target.value)\n",
    "            dfs(red.value, path=path, var=var)\n",
    "        elif red.find('NameNode') != -1:\n",
    "            if 'value' in red.__dict__.keys():\n",
    "                path.append('value')\n",
    "                # print('value深入')\n",
    "                dfs(red.value, path=path, var=var)\n",
    "            elif isinstance(red, Iterable): \n",
    "                # print('可迭代')\n",
    "                for index, i in enumerate(red):\n",
    "                    dfs(i, path=path+[index], var=var[:])\n",
    "        else:\n",
    "            default_value = red.value\n",
    "            if isinstance(default_value, Iterable):\n",
    "                ret['-'.join(var)] = BasicVarInfo(\n",
    "                    path=path,\n",
    "                    type=type(default_value),\n",
    "                    default=default_value,\n",
    "                    nargs='*',\n",
    "                    var=var\n",
    "                ),\n",
    "            else:\n",
    "                ret['-'.join(var)] = BasicVarInfo(\n",
    "                    path=path,\n",
    "                    type=type(default_value),\n",
    "                    default=default_value,\n",
    "                    var=var\n",
    "                ),\n",
    "                \n",
    "    dfs(red)\n",
    "                \n",
    "    return ret\n",
    "\n",
    "redbaron_config_parser(red)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(redbaron.nodes.AtomtrailersNode, False, True)"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new = assignment.value.value\n",
    "type(assignment.value), isinstance(new, abc.ABCMeta), 'value' not in new.__dict__.keys()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=====\n",
      "\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: name, name_, namenode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;204m=\u001b[39m\u001b[38;5;186m'dict'\u001b[39m\n",
      "=====\n",
      "\u001b[38;5;148mCallNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;245m# identifiers: call, call_, callnode\u001b[39m\n",
      "\u001b[38;5;15m  \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m CallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m CallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mAtomtrailersNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m CallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mAtomtrailersNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m CallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m    \u001b[39m\u001b[38;5;204m*\u001b[39m\u001b[38;5;81m CallArgumentNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;245m# identifiers: call_argument, call_argument_, callargument, callargumentnode\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mtarget\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n",
      "\u001b[38;5;15m        \u001b[39m\u001b[38;5;15mvalue\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m->\u001b[39m\n",
      "\u001b[38;5;15m          \u001b[39m\u001b[38;5;148mNameNode\u001b[39m\u001b[38;5;204m(\u001b[39m\u001b[38;5;204m)\u001b[39m\u001b[38;5;15m \u001b[39m\u001b[38;5;204m...\u001b[39m\n"
     ]
    }
   ],
   "source": [
    "for i in assignment.value.value[0]:\n",
    "    print('=====')\n",
    "    i.help()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
